package com.doradance.app.appactivity.search;


import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.doradance.R;
import com.doradance.account.ParamKey;
import com.doradance.account.UrlCenter;
import com.doradance.app.home.BabyDetailActivity;
import com.doradance.app.home.CoursesDetailActivity;
import com.doradance.app.home.FreeListAdapter;
import com.doradance.common.http.MyHttpRequest;
import com.doradance.entity.BabyListEntity;
import com.doradance.entity.FreeItemEntity;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.fragment.SwipeListFragment;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.framework.httprequest.responsecallbacks.ListDataRequestListener;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * A simple {@link Fragment} subclass.
 */
@SuppressLint("ValidFragment")
public class SearchBabyFragment extends SwipeListFragment {
    private String key;
    private int mPage = 0;

    public SearchBabyFragment(String key) {
        this.key = key;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        startLoading();
//        clearDecorations();
        return view;
    }

    @Override
    public void onStartRefreshing() {
        super.onStartRefreshing();
        getData().clear();
        mPage = 1;
        startLoading();
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long itemId) {
        super.onItemClick(arg0, arg1, position, itemId);
        BabyListEntity o = (BabyListEntity) getData().get(position);
        BabyDetailActivity.start(getActivity(),o.getId());

    }

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new BabyAdapter(getActivity());
    }

    private ListDataRequestListener<BabyListEntity> mResponceListener = new ListDataRequestListener<>(this, BabyListEntity.class, false);

    @Override
    public void onStartLoading() {
        if (mResponceListener.getResponseDataParser().hasMore()) {
            mPage++;
        }
        MyHttpRequest request = new MyHttpRequest(getContext());
        UrlParams urlParams = new UrlParams();
        urlParams.put(ParamKey.PAGE, mPage + "");
        urlParams.put(ParamKey.SIZE, 8 + "");
        urlParams.put(ParamKey.KEY, key);
        request.get(UrlCenter.BABYLIST, urlParams, mResponceListener);
    }

    @Override
    public View createEmptyView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.empty_search,null);
    }

    class BabyAdapter extends RecyclerViewBaseAdapter<BabyListEntity> {
        public BabyAdapter(Context con) {
            super(con);
        }

        @Override
        public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
            return new VH(setAdapterViewItem(parent, R.layout.baby_search_item));
        }

        @Override
        public void onBindHolder(RecyclerView.ViewHolder viewHolder, int position) {
            VH holder = (VH) viewHolder;
            BabyListEntity babyEntity = getData().get(position);
            holder.mTvBabySearchName.setText(babyEntity.getNick_name());
            holder.mTvBabySearchAge.setText(babyEntity.getAge_label());
            holder.mTvSearchSign.setText(babyEntity.getSign());
            Glide.with(getContext()).load(babyEntity.getAvatar()).into(holder.mCivSearch);
        }

        public class VH extends RecyclerView.ViewHolder {

            @Bind(R.id.civ_search)
            CircleImageView mCivSearch;
            @Bind(R.id.tv_baby_search_name)
            TextView mTvBabySearchName;
            @Bind(R.id.tv_baby_search_age)
            TextView mTvBabySearchAge;
            @Bind(R.id.tv_search_sign)
            TextView mTvSearchSign;
            public VH(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }

}
